extends Sprite

export(int) var speed = 75
var velocity = Vector2()

var stun = false

export(int) var hp = 3
export(int) var knockback = 400
export(int) var screen_shake = 10

onready var current_color = modulate

var blood = preload("res://prefabs/Blood.tscn")

func _process(delta):
	if hp <= 0:
		if Global.camera != null:
			Global.camera.screen_shake(screen_shake, 0.2)
		
		Global.points += 10
		if Global.node_creation_parent != null:
			var blood_istance = Global.instance_node(blood, global_position, Global.node_creation_parent)
			blood_istance.rotation = velocity.angle()
			blood_istance.modulate = Color.from_hsv(current_color.h, current_color.s, current_color.v * 0.5)
		queue_free()

func basic_movement_towards_player(delta):
	if Global.player != null and stun == false:
		velocity = global_position.direction_to(Global.player.global_position)
		global_position += velocity * speed * delta
	elif stun:
		velocity = lerp(velocity, Vector2(0, 0), 0.3)
		global_position += velocity * delta

func _on_Hitbox_area_entered(area):
	if area.is_in_group("Enemy_damager"):
		modulate = Color.white
		velocity = -velocity * knockback
		hp -= 1
		stun = true
		$Stun.start()
		area.get_parent().queue_free()

func _on_Stun_timeout():
	modulate = current_color
	stun = false
